/**
 * $Revision$
 * $Date$
 *
 * Copyright (C) 1999-2005 Jive Software. All rights reserved.
 * This software is the proprietary information of Jive Software. Use is subject to license terms.
 */

package com.suwish.pc.startup;

import java.io.File;
import java.io.FilenameFilter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * 
 * 一个简单的类加载器，从*.zip、*.jar中导入*.class文件
 *
 */
class UIClassLoader extends URLClassLoader {

    /**
     * 构造函数.ClassLoader继承URLClassLoader,可以加载指定jar或zip中的class到内存中
     *
     * @param parent the parent class loader (or null for none).
     * @param libDir  jar 文件路径.
     * @throws java.net.MalformedURLException 如果libDir路径无效.
     */
    UIClassLoader(ClassLoader parent, File libDir) throws MalformedURLException {
        super(new URL[]{libDir.toURI().toURL()}, parent);

        File[] jars = libDir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                boolean accept = false;
                String smallName = name.toLowerCase();
                if (smallName.endsWith(".jar")) {
                    accept = true;
                }
                else if (smallName.endsWith(".zip")) {
                    accept = true;
                }
                return accept;
            }
        });

        // 指定目录下没有jar或zip文件
        if (jars == null) {
            return;
        }

        for (File jar : jars) {
            if (jar.isFile()) {
                addURL(jar.toURI().toURL());
            }
        }
    }
}
